home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / tgasave / example.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-28  |  6.8 KB  |  264 lines

  1.  
  2. /**************************************************************************
  3.  *
  4.  *  FILE:           EXAMPLE.C
  5.  *
  6.  *  MODULE OF:      EXAMPLE
  7.  *
  8.  *  DESCRIPTION:    Example program using TGASAVE.
  9.  *
  10.  *                  Produces output to an EGA-screen, then dumps it to
  11.  *                  a TGA-file.
  12.  *
  13.  *                  This program is rather slow. The bottleneck is
  14.  *                  Borland's getpixel() -function, not the TGASAVE-
  15.  *                  functions.
  16.  *
  17.  *                  Since TGASAVE outputs truecolor TGA-files, it's rather
  18.  *                  `stupid' to use that format on EGA-screen images.
  19.  *                  Take this as an example only!
  20.  *
  21.  *  WRITTEN BY:     Sverre H. Huseby
  22.  *
  23.  **************************************************************************/
  24.  
  25.  
  26.  
  27. #ifndef __TURBOC__
  28.   #error This program must be compiled using a Borland C compiler
  29. #endif
  30.  
  31.  
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <graphics.h>
  35.  
  36. #include "tgasave.h"
  37.  
  38.  
  39. #define NUMCOLORS 16            /* Number of EGA-colors. */
  40.  
  41.  
  42.  
  43. /**************************************************************************
  44.  *                                                                        *
  45.  *                       P R I V A T E    D A T A                         *
  46.  *                                                                        *
  47.  **************************************************************************/
  48.  
  49. static int
  50.     Red[NUMCOLORS],             /* Red component for each color. */
  51.     Green[NUMCOLORS],           /* Green component for each color. */
  52.     Blue[NUMCOLORS];            /* Blue component for each color. */
  53.  
  54.  
  55.  
  56.  
  57.  
  58. /**************************************************************************
  59.  *                                                                        *
  60.  *                   P R I V A T E    F U N C T I O N S                   *
  61.  *                                                                        *
  62.  **************************************************************************/
  63.  
  64. /*-------------------------------------------------------------------------
  65.  *
  66.  *  NAME:           DrawScreen()
  67.  *
  68.  *  DESCRIPTION:    Produces some output on the graphic screen.
  69.  *
  70.  *  PARAMETERS:     None
  71.  *
  72.  *  RETURNS:        Nothing
  73.  *
  74.  */
  75. static void DrawScreen(void)
  76. {
  77.     int  color = 1, x, y;
  78.     char *text = "TGA-file produced by TGASAVE";
  79.  
  80.  
  81.     /*
  82.      *  Output some lines
  83.      */
  84.     setlinestyle(SOLID_LINE, 0, 3);
  85.     for (x = 10; x < getmaxx(); x += 20) {
  86.         setcolor(color);
  87.         line(x, 0, x, getmaxy());
  88.         if (++color > getmaxcolor())
  89.             color = 1;
  90.     }
  91.     for (y = 8; y < getmaxy(); y += 17) {
  92.         setcolor(color);
  93.         line(0, y, getmaxx(), y);
  94.         if (++color > getmaxcolor())
  95.             color = 1;
  96.     }
  97.  
  98.     /*
  99.      *  And then some text
  100.      */
  101.     setfillstyle(SOLID_FILL, DARKGRAY);
  102.     settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4);
  103.     bar(20, 10, textwidth(text) + 40, textheight(text) + 20);
  104.     setcolor(WHITE);
  105.     outtextxy(30, 10, text);
  106. }
  107.  
  108.  
  109.  
  110. /*-------------------------------------------------------------------------
  111.  *
  112.  *  NAME:           SplitPalette()
  113.  *
  114.  *  DESCRIPTION:    Interpret the EGA-palette, extracting red-, green- and
  115.  *                  blue-values. This is what we need for TrueColor.
  116.  *
  117.  *                  The extracted values are stored in global variables.
  118.  *
  119.  *  PARAMETERS:     None
  120.  *
  121.  *  RETURNS:        Nothing
  122.  *
  123.  */
  124. static void SplitPalette(void)
  125. {
  126.     int q,                      /* Counter */
  127.         color;                  /* Temporary color value */
  128.     struct palettetype pal;
  129.  
  130.  
  131.     /*
  132.      *  Get the color palette, and extract the red, green and blue
  133.      *  components for each color. In the EGA palette, colors are
  134.      *  stored as bits in bytes:
  135.      *
  136.      *      00rgbRGB
  137.      *
  138.      *  where r is low intensity red, R is high intensity red, etc.
  139.      *  We shift the bits in place like
  140.      *
  141.      *      Rr000000
  142.      *
  143.      *  for each component
  144.      */
  145.     getpalette(&pal);
  146.     for (q = 0; q < NUMCOLORS; q++) {
  147.         color = pal.colors[q];
  148.         Red[q]   = ((color & 4) << 5) | ((color & 32) << 1);
  149.         Green[q] = ((color & 2) << 6) | ((color & 16) << 2);
  150.         Blue[q]  = ((color & 1) << 7) | ((color & 8) << 3);
  151.     }
  152. }
  153.  
  154.  
  155.  
  156. /*-------------------------------------------------------------------------
  157.  *
  158.  *  NAME:           GetPixel()
  159.  *
  160.  *  DESCRIPTION:    Callback function that retreives the pixel color value.
  161.  *
  162.  *  PARAMETERS:     x, y   Image pixel location.
  163.  *                  col    Pointer to color structure.
  164.  *
  165.  *  RETURNS:        Nothing
  166.  *
  167.  */
  168. static void GetPixel(int x, int y, TGA_Color *col)
  169. {
  170.     int idx;
  171.  
  172.  
  173.     idx = getpixel(x, y);
  174.     col->r = Red[idx];
  175.     col->g = Green[idx];
  176.     col->b = Blue[idx];
  177. }
  178.  
  179.  
  180.  
  181. /*-------------------------------------------------------------------------
  182.  *
  183.  *  NAME:           TGA_DumpEga10()
  184.  *
  185.  *  DESCRIPTION:    Outputs a graphics screen to a TGA-file. The screen
  186.  *                  must be in the mode 0x10, EGA 640x350, 16 colors.
  187.  *
  188.  *                  No error checking is done! Probably not a very good
  189.  *                  example, then . . . :-)
  190.  *
  191.  *  PARAMETERS:     filename   Name of TGA-file.
  192.  *
  193.  *  RETURNS:        Nothing
  194.  *
  195.  */
  196. static void TGA_DumpEga10(char *filename)
  197. {
  198.   #define WIDTH            640  /* 640 pixels across screen */
  199.   #define HEIGHT           350  /* 350 pixels down screen */
  200.  
  201.  
  202.     /*
  203.      *  Get the current palette.
  204.      */
  205.     SplitPalette();
  206.  
  207.     /*
  208.      *  Set up some variables that are included in the file.
  209.      *  This is not neccesary.
  210.      */
  211.     TGA_SetImageID("Example image");
  212.     TGA_SetAuthor("Sverre H. Huseby");
  213.     TGA_SetSoftwareID("EXAMPLE");
  214.  
  215.     /*
  216.      *  Create the TGA-file.
  217.      */
  218.     TGA_Save(filename, WIDTH, HEIGHT, GetPixel);
  219. }
  220.  
  221.  
  222.  
  223.  
  224.  
  225. /**************************************************************************
  226.  *                                                                        *
  227.  *                    P U B L I C    F U N C T I O N S                    *
  228.  *                                                                        *
  229.  **************************************************************************/
  230.  
  231. int main(void)
  232. {
  233.     int gdr, gmd, errcode;
  234.  
  235.  
  236.     /*
  237.      *  Initiate graphics screen for EGA mode 0x10, 640x350x16
  238.      */
  239.     gdr = EGA;
  240.     gmd = EGAHI;
  241.     initgraph(&gdr, &gmd, "");
  242.     if ((errcode = graphresult()) != grOk) {
  243.         printf("Graphics error: %s\n", grapherrormsg(errcode));
  244.         exit(-1);
  245.     }
  246.  
  247.     /*
  248.      *  Put something on the screen
  249.      */
  250.     DrawScreen();
  251.  
  252.     /*
  253.      *  Dump the screen to a TGA-file
  254.      */
  255.     TGA_DumpEga10("EXAMPLE.TGA");
  256.  
  257.     /*
  258.      *  Return to text mode
  259.      */
  260.     closegraph();
  261.  
  262.     return 0;
  263. }
  264.